package com.xxxt.utils;

import java.util.Date;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.UnsupportedEncodingException;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

/**
 * 
 * @author huangrusiehng
 *
 */
public class MailUtil {
	static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	static final String SMTPSERVER = "smtp.163.com";
	static final String PORT = "465";
	static final String USERNAME = "a18577140506@163.com";
	static final String PASSWORD = "UEHAUHTEABAJJXJO";

	/**
	 * 
	 * 进行base64加密，防止中文乱码
	 * 
	 */
	public static String changeEncode(String str) {
		try {
		str = MimeUtility.encodeText(new String(str.getBytes(), "UTF-8"),"UTF-8", "B"); // "B"代表Base64
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
	public static void send(String to,String title,String content) {
		Properties properties = new Properties();
		properties.put("mail.smtp.host", SMTPSERVER);
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.socketFactory.class", SSL_FACTORY); // 使用JSSE的SSL
		properties.put("mail.smtp.socketFactory.fallback", "false"); // 只处理SSL的连接,对于非SSL的连接不做处理
		properties.put("mail.smtp.port", PORT);
		properties.put("mail.smtp.socketFactory.port", PORT);
		properties.put("mail.smtp.ssl.enable", true);
		Session session = Session.getInstance(properties);
		session.setDebug(true);
		MimeMessage message = new MimeMessage(session);
		try {
			// 发件人
			Address address = new InternetAddress(USERNAME);
			message.setFrom(address);
			// 收件人
			Address toAddress = new InternetAddress(to);
			message.setRecipient(MimeMessage.RecipientType.TO, toAddress); // 设置收件人,并设置其接收类型为TO
			// 主题
			message.setSubject(changeEncode(title));
			// 时间
			message.setSentDate(new Date());
			Multipart multipart = new MimeMultipart();
			// 添加文本
			BodyPart text = new MimeBodyPart();
			text.setText(content);
			multipart.addBodyPart(text);
			message.setContent(multipart);
			message.saveChanges();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			Transport transport = session.getTransport("smtp");
			transport.connect(SMTPSERVER, USERNAME, PASSWORD);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static boolean checkemail(String email) {
		Pattern pattern = Pattern.compile("^^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
		Matcher matcher = pattern.matcher(email);
		boolean result = matcher.matches();
		if(result) {
			return true;
		}else {
		return false;
		}
	}
	public static boolean checkphone(String phone) {
		Pattern pattern = Pattern.compile("^1[0-9]{10}$");
		Matcher matcher = pattern.matcher(phone);
		boolean result = matcher.matches();
		if(result) {
			return true;
		}else {
			return false;
			}
		}
	public static boolean checkname(String name) {
		Pattern pattern = Pattern.compile("^[\\u4e00-\\u9fa5]{2,4}$");
		Matcher matcher = pattern.matcher(name);
		boolean result = matcher.matches();
		if(result) {
			return true;
		}else {
		return false;
		}
	}
	public static boolean checksex(String name) {
		Pattern pattern = Pattern.compile("^[\\u4e00-\\u9fa5]{1,2}$");
		Matcher matcher = pattern.matcher(name);
		boolean result = matcher.matches();
		if(result) {
			return true;
		}else {
			return false;
		}
	}

}
